package com.ruoyi.eduadmin.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.eduadmin.domain.ItClasses;
import com.ruoyi.eduadmin.service.IItClassesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 班级管理Controller
 * 
 * @author ruoyi
 * @date 2022-12-26
 */
@RestController
@RequestMapping("/class")
public class ItClassesController extends BaseController
{
    @Autowired
    private IItClassesService itClassesService;

    /**
     * 查询班级管理列表
     */
    @RequiresPermissions("eduadmin:class:list")
    @GetMapping("/list")
    public TableDataInfo list(ItClasses itClasses)
    {
        startPage();
        List<ItClasses> list = itClassesService.selectItClassesList(itClasses);
        return getDataTable(list);
    }

    /**
     * 导出班级管理列表
     */
    @RequiresPermissions("eduadmin:class:export")
    @Log(title = "班级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItClasses itClasses)
    {
        List<ItClasses> list = itClassesService.selectItClassesList(itClasses);
        ExcelUtil<ItClasses> util = new ExcelUtil<ItClasses>(ItClasses.class);
        util.exportExcel(response, list, "班级管理数据");
    }

    /**
     * 获取班级管理详细信息
     */
    @RequiresPermissions("eduadmin:class:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(itClassesService.selectItClassesById(id));
    }

    /**
     * 新增班级管理
     */
    @RequiresPermissions("eduadmin:class:add")
    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItClasses itClasses)
    {
        //获取年份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(itClasses.getStartDate());	//放入Date类型数据
        int year = calendar.get(Calendar.YEAR);//获取年份
        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
        int day = calendar.get(Calendar.DATE);//获取日
        //组装班级名
        itClasses.setClassName("[" + year + "]" + month + "月" + day + "日" + itClasses.getClassName());
        return toAjax(itClassesService.insertItClasses(itClasses));
    }

    /**
     * 修改班级管理
     */
    @RequiresPermissions("eduadmin:class:edit")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItClasses itClasses)
    {
        //获取年份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(itClasses.getStartDate());					//放入Date类型数据
        int year = calendar.get(Calendar.YEAR);//获取年份
        int month = calendar.get(Calendar.MONTH) + 1;//获取月份
        int day = calendar.get(Calendar.DATE);//获取日
        //组装班级名
        itClasses.setClassName("[" + year + "]" + month + "月" + day + "日" + itClasses.getClassName().substring(itClasses.getClassName().indexOf('日')+1));
        return toAjax(itClassesService.updateItClasses(itClasses));
    }

    /**
     * 删除班级管理
     */
    @RequiresPermissions("eduadmin:class:remove")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(itClassesService.deleteItClassesByIds(ids));
    }

    /**
     * 查询未开班的班级
     */
    @RequiresPermissions("eduadmin:class:list")
    @Log(title = "班级管理", businessType = BusinessType.OTHER)
    @GetMapping("/listAll/{status}")
    public AjaxResult listAll(@PathVariable("status") Integer[] stateIds)
    {
        List<ItClasses> list = new ArrayList<>();
        for (Integer stateId : stateIds) {
            ItClasses itClasses = new ItClasses();
            itClasses.setStateId(stateId);
            list.addAll(itClassesService.selectItClassesList(itClasses));
        }
        return success(list);
    }
}
